﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfControlLibrary
{
    /// <summary>
    /// CircleCheckValve.xaml 的交互逻辑
    /// </summary>
    public partial class CircleCheckValve : UserControl
    {
        public CircleCheckValve()
        {
            InitializeComponent();
            UpdateDirection();
            UpdateBackgroundColor();
            UpdateColor(); // 添加这行确保初始颜色正确
        }

        #region 依赖属性

        // 阀门名称
        public string ValveName
        {
            get { return (string)GetValue(ValveNameProperty); }
            set { SetValue(ValveNameProperty, value); }
        }

        public static readonly DependencyProperty ValveNameProperty =
            DependencyProperty.Register("ValveName", typeof(string), typeof(CircleCheckValve),
                new PropertyMetadata("CV-001"));

        // 阀体颜色（线路颜色）
        public Brush BodyColor
        {
            get { return (Brush)GetValue(BodyColorProperty); }
            set { SetValue(BodyColorProperty, value); }
        }

        public static readonly DependencyProperty BodyColorProperty =
            DependencyProperty.Register("BodyColor", typeof(Brush), typeof(CircleCheckValve),
                new PropertyMetadata(Brushes.SteelBlue, OnColorChanged));

        // 文字颜色（独立于线路颜色）
        public Brush TextColor
        {
            get { return (Brush)GetValue(TextColorProperty); }
            set { SetValue(TextColorProperty, value); }
        }

        public static readonly DependencyProperty TextColorProperty =
            DependencyProperty.Register("TextColor", typeof(Brush), typeof(CircleCheckValve),
                new PropertyMetadata(Brushes.DarkBlue));

        // 正常状态背景色
        public Brush NormalBackground
        {
            get { return (Brush)GetValue(NormalBackgroundProperty); }
            set { SetValue(NormalBackgroundProperty, value); }
        }

        public static readonly DependencyProperty NormalBackgroundProperty =
            DependencyProperty.Register("NormalBackground", typeof(Brush), typeof(CircleCheckValve),
                new PropertyMetadata(Brushes.White, OnBackgroundChanged));

        // 关闭状态背景色
        public Brush ClosedBackground
        {
            get { return (Brush)GetValue(ClosedBackgroundProperty); }
            set { SetValue(ClosedBackgroundProperty, value); }
        }

        public static readonly DependencyProperty ClosedBackgroundProperty =
            DependencyProperty.Register("ClosedBackground", typeof(Brush), typeof(CircleCheckValve),
                new PropertyMetadata(Brushes.LightGray, OnBackgroundChanged));

        // 流动方向 (true: 从右向左, false: 从左向右)
        public bool IsReverseFlow
        {
            get { return (bool)GetValue(IsReverseFlowProperty); }
            set { SetValue(IsReverseFlowProperty, value); }
        }

        public static readonly DependencyProperty IsReverseFlowProperty =
            DependencyProperty.Register("IsReverseFlow", typeof(bool), typeof(CircleCheckValve),
                new PropertyMetadata(true, OnFlowDirectionChanged));

        private static void OnFlowDirectionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as CircleCheckValve;
            control?.UpdateDirection();
        }

        // 阀门状态
        public bool IsClosed
        {
            get { return (bool)GetValue(IsClosedProperty); }
            set { SetValue(IsClosedProperty, value); }
        }

        public static readonly DependencyProperty IsClosedProperty =
            DependencyProperty.Register("IsClosed", typeof(bool), typeof(CircleCheckValve),
                new PropertyMetadata(true, OnValveStateChanged));

        private static void OnValveStateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as CircleCheckValve;
            control?.UpdateValveState();
            control?.UpdateBackgroundColor();
        }

        private static void OnColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as CircleCheckValve;
            control?.UpdateColor();
        }

        private static void OnBackgroundChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var control = d as CircleCheckValve;
            control?.UpdateBackgroundColor();
        }

        #endregion

        #region 方法

        private void UpdateDirection()
        {
            if (IsReverseFlow)
            {
                // 从右向左流动（默认）
                ValveArrow.RenderTransform = null;
            }
            else
            {
                // 从左向右流动
                ValveArrow.RenderTransform = new ScaleTransform(-1, 1, 20, 10);
            }
        }

        private void UpdateValveState()
        {
            ClosedIndicator.Visibility = IsClosed ? Visibility.Visible : Visibility.Collapsed;
            // 移除箭头和线条颜色的修改逻辑
            // 只保留关闭指示线的显示/隐藏
            //if (IsClosed)
            //{
            //    // 关闭状态：红色
            //    ValveArrow.Fill = Brushes.Red;
            //    ValveLine.Stroke = Brushes.Red;
            //}
            //else
            //{
            //    // 正常状态：使用BodyColor
            //    UpdateColor();
            //}
        }

        private void UpdateColor()
        {
            //if (!IsClosed)
            //{
            //    ValveArrow.Fill = BodyColor;
            //    ValveLine.Stroke = BodyColor;
            //}
            // 无论阀门状态如何，都使用BodyColor
            ValveArrow.Fill = BodyColor;
            ValveLine.Stroke = BodyColor;
        }

        private void UpdateBackgroundColor()
        {
            // 根据阀门状态设置背景色
            OuterCircle.Fill = IsClosed ? ClosedBackground : NormalBackground;
        }

        #endregion
    }
}
